package com.ericsson.eritask;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.ericsson.eritask.domain.Task;
import com.ericsson.eritask.domain.User;
import com.ericsson.eritask.domain.UserProject;
import com.ericsson.eritask.domain.UserTask;
import com.ericsson.eritask.dto.MyTaskDTO;
import com.ericsson.eritask.service.LoginService;
import com.ericsson.eritask.service.MyTaskService;
import com.ericsson.eritask.service.UserService;

@Controller
public class ValidateUserController {

	@Autowired	
	LoginService loginService;

	@Autowired
	MyTaskService myTaskService;

	@Autowired
	UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public void setMyTaskService(MyTaskService myTaskService) {
		this.myTaskService = myTaskService;
	}


	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	@RequestMapping(value="/populatedImage",method = RequestMethod.GET,produces=MediaType.IMAGE_JPEG_VALUE)
	@ResponseBody
	public byte[] retrieveImage(HttpServletRequest request) throws IOException{
		System.out.println("in retrieveImage() .....");
		Authentication auth= SecurityContextHolder.getContext().getAuthentication();

		System.out.format("Name of the user is %s", auth.getName());
		User user = userService.findByName(auth.getName());
		String signum = user.getSignum();
		//String signum = request.getSession().getAttribute("user_signum").toString();
		InputStream input = userService.retrieveImage(signum);
		final HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_JPEG);
		return IOUtils.toByteArray(input);

	}
	

	@RequestMapping(value="/uploadUserImage", method = {RequestMethod.POST},consumes={"multipart/form-data"})
	public void uploadUserImage(@RequestParam("b12") MultipartFile multipartFile,MultipartHttpServletRequest request,HttpServletResponse response) throws IOException{
		System.out.println("in uploadUserImage().........");
		Authentication auth= SecurityContextHolder.getContext().getAuthentication();

		System.out.format("Name of the user is %s", auth.getName());
		User user = userService.findByName(auth.getName());
		String signum = user.getSignum();
		//String signum = request.getSession().getAttribute("user_signum").toString();
		InputStream uploadedFileRef =  multipartFile.getInputStream();
		System.out.println("signum"+signum);
		String filename = request.getContextPath().toString();
		String imageId = userService.uploadImage(uploadedFileRef, signum,filename);
		System.out.println("imageId in uploaduserimage()"+imageId);
		response.sendRedirect("validateUser");
		//return "MyTask";

	}

	@RequestMapping(value="/validateUser", method = {RequestMethod.POST, RequestMethod.GET})
	public String validateUserCredential(HttpServletRequest request,Map<String, String> map,Model  model){
		System.out.println("calling validateUser controller..");
		
		System.out.println("in validateUserCredential()"+request.getSession().getAttribute("user_signum"));
		String page = null;
		String message = null;
		//List<MyTaskDTO> taskDTO =null;
		Authentication auth= SecurityContextHolder.getContext().getAuthentication();
		List<MyTaskDTO> allTaskList = new ArrayList<MyTaskDTO>();

		System.out.format("Name of the user is %s", auth.getName());
		User user = userService.findByName(auth.getName());
		map.put("signum", user.getSignum());
		allTaskList = populateMyTaskList(user.getSignum());
		model.addAttribute("mytaskDTO", allTaskList);
		// request.getSession().setAttribute("AllTaskList", allTaskList);
		page= "MyTask";

		
		return page;   
	}



	@RequestMapping(value="/populate",method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public @ResponseBody List<MyTaskDTO> populateMyTaskList(String signum )
	{
		Map<Integer , List<MyTaskDTO>> project_taskMap = new HashMap<Integer, List<MyTaskDTO>>();


		List<MyTaskDTO> taskDTO =new ArrayList<MyTaskDTO>();
		List<MyTaskDTO> allTaskList = new ArrayList<MyTaskDTO>();
		List<UserTask> usertask_list  =  myTaskService.getUserTaskList(signum);

		Task task= null;
		//if(userproject_list!=null && userproject_list.size()>0 && usertask_list!=null && usertask_list.size()>0){
		if(usertask_list!=null && usertask_list.size()>0){
			/*for(UserProject up :userproject_list){

				int  proj_id = up.getProject().getProjectId();
				System.out.println("proj_id in controller "+proj_id);*/

			for(UserTask uTask :usertask_list){

				task = new Task();

				task = uTask.getTask();
				//int  task_id = task.getTaskId();
				int  proj_id = task.getProject().getProjectId();


				MyTaskDTO myTaskDTO = new MyTaskDTO();
				myTaskDTO.setProjectId(proj_id);
				myTaskDTO.setProjectName(task.getProject().getProjectName());
				myTaskDTO.setTaskId(task.getTaskId());
				myTaskDTO.setSubject(task.getSubject());
				myTaskDTO.setStartDate(task.getStartDate());
				myTaskDTO.setPriority(task.getPriority());
				myTaskDTO.setStatus(task.getStatus());
				myTaskDTO.setDueDate(task.getDueDate());
				myTaskDTO.setPercentageComplete(task.getPercentageComplete());
				//myTaskDTO.setSignum(user.add(uTask.getUser().getSignum()));
				allTaskList.add(myTaskDTO);

				//taskDTO = myTaskService.populateMyTaskList(proj_id);
				//taskDTO = myTaskService.populateMyUserTaskList(projId)
				project_taskMap.put(proj_id, taskDTO);
				allTaskList.addAll(taskDTO);
				System.out.println("taskDTO size in controller "+taskDTO.size());
				System.out.println("allTaskList size in controller "+allTaskList.size());
			}
		}
		//		}

		return allTaskList;
	}

}







